/*
* @author : TF-BJ-C064
* @creation : 2014-6-27 下午4:30:37
* @description : 
*
*/

package com.action.park;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.bean.ParkManager;
import com.dao.ParkOrderDao;
import com.service.ParkManagerService;
import com.util.EncoderHandler;
import com.util.GUtil;
import com.util.ValidateUtil;

public class ParkManagerAction extends BaseParkAction{

	private static final Logger log = Logger.getLogger(ParkOrderDao.class);
	 
	ParkManagerService service;
	ParkManager parkManager;
	List<ParkManager> list;
	Integer flag;
	Timestamp ts = new Timestamp(System.currentTimeMillis());
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
	String date = sdf.format(ts);
	Boolean queryMyOrder=true;//true: 收费员查询自己的订单 ; false查询所有订单
	
	@Override
	public String handle() throws Exception {
		this.setHeaderTitle("停车场会员管理");
		if(id==null)
			return this.query();
		else
			return this.view();
	}
	/**
	 * @return  绩效统计
	 * @throws Exception
	 * @see com.action.park.BaseParkAction#query()
	 */
	@Override
	public String query() throws Exception {
		try {
			list = service.findbyrole(parkManager,page);
		} catch (Exception e) {
			e.getStackTrace();
			return GUtil.ERROR;
		}
		return GUtil.SUCCESS;
	}
	@Override
	public String view() throws Exception {
		if(id==null || id<1){
			return GUtil.ERROR;
		}
		parkManager = service.get((long)id);
		return GUtil.VIEW;
	}
	
	public String checkRealkName() throws IOException{
		JSONObject json = new JSONObject();
		try {
			String ret = service.findNames(parkManager.getUserName());
			if(ret==null){
				json.put("error", 0);
			}else {
				json.put("error", -1);
				json.put("msg", "用户已存在");
			}
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.close();
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			return GUtil.ERROR;
		}
		return GUtil.NONE;
	}
	@Override
	public String add() throws Exception {
		this.setHeaderTitle("添加停车场会员");
		
		//beforeAdd
		if(parkManager==null){
			//beforeUpdate
			flag = 0;
			return GUtil.EDIT;
		}
		
		//用户名、密码、真实姓名等后台校验
		if(!this.validate(parkManager)){
			return GUtil.EDIT;
		}
		//add
		ParkManager p=new ParkManager();
		p.setCreateTime(ts);
		p.setPhoto(parkManager.getPhoto());
		p.setPhone(parkManager.getPhone());
		p.setRealName(parkManager.getRealName());
		p.setUserName(parkManager.getUserName());
		p.setIdCard(parkManager.getIdCard());
		p.setRole(Short.valueOf((short) 3));
		p.setBankAccount(parkManager.getBankAccount());
		p.setIntroduction(parkManager.getIntroduction());
		p.setStatus(Short.valueOf((short) 1));
		
		if(!parkManager.getUserPasswd().trim().isEmpty()){
			p.setUserPasswd(EncoderHandler.EncoderPasswordBase64Md5(parkManager.getUserPasswd()));
		}
		
		if(service.save(p).endsWith(GUtil.SUCCESS)){
			this.setMsg("添加成功！");
			parkManager=null;
			return this.query();
		}else{
			this.setMsg("添加失败！");
			return GUtil.ERROR;
		}
	}
	
	public String lookview() throws Exception {
		if (id == null || id < 1)
			return GUtil.ERROR;
		parkManager = (ParkManager) service.get(Long.valueOf(id));
		if (parkManager == null)
			return GUtil.ERROR;
		return GUtil.VIEW;
	}
	@Override
	public String update() throws Exception {
		this.setHeaderTitle("确认修改");
		if(id==null){
			return GUtil.ERROR;
		}
		if(parkManager==null){
			// beforeUpdate
			parkManager=service.get(Long.valueOf(id));
			if(parkManager==null){
				this.setLogmsg("信息不存在 id=" + id);
				return GUtil.ERROR;
			}
			session.put("parkManager", parkManager);
			session.put("id", parkManager.getUserId());
			return GUtil.EDIT;
		}
		//用户名、密码、真实姓名等后台校验
		if(!this.validate(parkManager)){
			return GUtil.EDIT;
		}
		
		//to update
		ParkManager pm=(ParkManager) session.get("parkManager");
		pm.setPhone(parkManager.getPhone());
		pm.setPhoto(parkManager.getPhoto());
		pm.setIdCard(parkManager.getIdCard());
		pm.setRealName(parkManager.getRealName());
		
//		pm.setUserName(parkManager.getUserName());//用户帐号允许修改，你的QQ在修改个人信息时运行修改qq号码？
		
		pm.setUpdateTime(ts);
		pm.setBankAccount(parkManager.getBankAccount());
		pm.setIntroduction(parkManager.getIntroduction());
		if(parkManager.getUserPasswd()!=null && !parkManager.getUserPasswd().trim().isEmpty()){
			pm.setUserPasswd(EncoderHandler.EncoderPasswordBase64Md5(parkManager.getUserPasswd()));
		}else{
			pm.setUserPasswd(parkManager.getUserPasswd());
		}
		if (service.update(pm).endsWith(GUtil.SUCCESS)) {
			this.setMsg("修改成功");
			pm = null;
			list = service.findbyrole(null,page);
			return GUtil.SUCCESS;
		} else {
			this.setMsg("修改失败");
			return GUtil.EDIT;
		}
	}

 
	public Boolean getQueryMyOrder() {
		return queryMyOrder;
	}

	public void setQueryMyOrder(Boolean queryMyOrder) {
		this.queryMyOrder = queryMyOrder;
	}

	public ParkManagerService getService() {
		return service;
	}

	public void setService(ParkManagerService service) {
		this.service = service;
	}

	public ParkManager getManager() {
		return parkManager;
	}

	public void setManager(ParkManager manager) {
		this.parkManager = manager;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((list == null) ? 0 : list.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ParkManagerAction other = (ParkManagerAction) obj;
		if (list == null) {
			if (other.list != null)
				return false;
		} else if (!list.equals(other.list))
			return false;
		return true;
	}

	public List<ParkManager> getList() {
		return list;
	}

	public void setList(List<ParkManager> list) {
		this.list = list;
	}

	@Override
	public String delete() throws Exception {
		session.put("userinfo", service.get(Long.valueOf(id)));
		ParkManager or = (ParkManager) session.get("userinfo");
		if(id==null || id<1)
			return GUtil.ERROR;
		else{
			if(service.delete(Long.valueOf(id),-1).equals(GUtil.SUCCESS)){
				this.setMsg("删除成功");
				this.setLogmsg("[" + date + "][后台管理员:" + user.getUserCode() + ","
						+ user.getUserName() + "][把用户:"
						+ or.getRealName() + "成功删除");
			}else{
				this.setMsg("删除失败");
				this.setLogmsg("[" + date + "][后台管理员:" + user.getUserCode() + ","
						+ user.getUserName() + "][删除用户:"
						+ or.getRealName() + "失败");
			}
		}
		return this.query() ;
	}
	/*
	 * 锁定
	 */
	public String locking() throws Exception {
		session.put("manager", service.get(Long.valueOf(id)));
		ParkManager or = (ParkManager) session.get("manager");
		if(id==null || id<1)
			return GUtil.ERROR;
		else{
			if(service.lock(Long.valueOf(id),0).equals(GUtil.SUCCESS)){
				this.setMsg("锁定成功");
				this.setLogmsg("[" + date + "][后台管理员:" + user.getUserCode() + ","
						+ user.getUserName() + "][把用户:"
						+ or.getRealName() + "已锁定");
			}else{
				this.setMsg("锁定失败");
				this.setLogmsg("[" + date + "][后台管理员:" + user.getUserCode() + ","
						+ user.getUserName() + "][把用户:"
						+ or.getRealName() + "锁定失败");
			}
		}
		return this.query() ;
	}
	/*
	 * 解锁
	 */
	public String delocking() throws Exception {
		session.put("manager", service.get(Long.valueOf(id)));
		ParkManager or = (ParkManager) session.get("manager");
		if(id==null || id<1)
			return GUtil.ERROR;
		else{
			if(service.lock(Long.valueOf(id),1).equals(GUtil.SUCCESS)){
				this.setMsg("成功解锁");
				this.setLogmsg("[" + date + "][后台管理员:" + user.getUserCode() + ","
					+ user.getUserName() + "][把用户:"
					+ or.getRealName() + "已成功解锁");
			}else{
				this.setMsg("解锁失败");
				this.setLogmsg("[" + date + "][后台管理员:" + user.getUserCode() + ","
					+ user.getUserName() + "][把用户:"
					+ or.getRealName() + "已成功解锁");
			}
		}
		return this.query() ;
	}
	public Integer getFlag() {
		return flag;
	}
	public void setFlag(Integer flag) {
		this.flag = flag;
	}
	@Override
	public boolean validate(Object obj) throws Exception {
		boolean bok = true;
		if(ValidateUtil.chekcUserName(parkManager.getUserName())==false){
			this.addFieldError("manager.userName", "用户名不符合规范");
			bok &= false ;
		}
		if(ValidateUtil.chekcPassword(parkManager.getUserPasswd())==false){
			this.addFieldError("manager.userPassowrd", "密码不符合规范");
			bok &= false ;
		}
		if(ValidateUtil.chekcRealName(parkManager.getRealName())==false){
			this.addFieldError("manager.realName", "真实姓名不符合规范");
			bok &= false ;
		}
		
		///***   添加其他的校验
		//  .....
		
		return bok;
	}
	public ParkManager getParkManager() {
		return parkManager;
	}
	public void setParkManager(ParkManager parkManager) {
		this.parkManager = parkManager;
	}


}
